package com.scada.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scada.contant.MessageConstants;
import com.scada.hessian.client.impl.DictionaryDataVisitorClient;
import com.scada.hessian.client.impl.DictionaryVisitorClient;
import com.scada.model.bean.Dictionary;
import com.scada.model.bean.DictionaryData;
import com.scada.model.bean.vo.DictionaryVO;
import com.scada.model.result.ActionResult;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.model.result.RestResponseBean;
import com.scada.service.DicService;

@Service
public class DicServiceImpl implements DicService {
	
	@Autowired
	private DictionaryDataVisitorClient dictionaryDataVisitorClient;
	
	@Autowired
	private DictionaryVisitorClient dictionaryVisitorClient;

	@Override
	public PageInfo<Dictionary> selectAll(PageInfo<Dictionary> pageInfo) {

		return dictionaryVisitorClient.selectAll(pageInfo);
	}

	@Override
	public PageInfo<DictionaryVO> selectAllData(PageInfo<DictionaryVO> pageInfo) {

		return  dictionaryDataVisitorClient.selectAll(pageInfo);
	}

	@Override
	public ActionResult insertSelective(Dictionary record) {
		record.setState(MessageConstants.State.STATE_SHOW.getKey());
		return dictionaryVisitorClient.insertSelective(record);
	}

	@Override
	public ActionResult updateByPrimaryKeySelective(Dictionary record) {
		record.setState(MessageConstants.State.STATE_SHOW.getKey());
		return dictionaryVisitorClient.updateByPrimaryKeySelective(record);
	}

	@Override
	public Dictionary selectByPrimaryKey(int id) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("state", MessageConstants.State.STATE_SHOW.getKey());
		map.put("id", id);
		return dictionaryVisitorClient.selectByPrimaryKey(map);
	}

	@Override
	public ActionResult insertSelectiveD(DictionaryData record) {
		record.setState(MessageConstants.State.STATE_SHOW.getKey());
		return dictionaryDataVisitorClient.insertSelective(record);
	}

	@Override
	public DictionaryData selectByPrimaryKeyD(int id) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("state", MessageConstants.State.STATE_SHOW.getKey());
		map.put("id", id);
		return dictionaryDataVisitorClient.selectByPrimaryKey(map);
	}

	@Override
	public ActionResult updateByPrimaryKeySelectiveD(DictionaryData record) {
		record.setState(MessageConstants.State.STATE_SHOW.getKey());
		return dictionaryDataVisitorClient.updateByPrimaryKeySelective(record);
	}

	@Override
	public ActionResult deleteByPrimaryKey(String ids) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("state", MessageConstants.State.STATE_DEL.getKey());
		List<Integer> empNoList=new ArrayList<Integer>();
		for(String empno : ids.split(","))
		{
			empNoList.add(Integer.parseInt(empno));
		}
		map.put("empNoList", empNoList);
		return dictionaryVisitorClient.deleteByPrimaryKey(map);
	}

	@Override
	public ActionResult deleteByPrimaryKeyD(String ids) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("state", MessageConstants.State.STATE_DEL.getKey());
		List<Integer> empNoList=new ArrayList<Integer>();
		for(String empno : ids.split(","))
		{
			empNoList.add(Integer.parseInt(empno));
		}
		map.put("empNoList", empNoList);
		return dictionaryDataVisitorClient.deleteByPrimaryKey(map);
	}

	@Override
	public List<Dictionary> list(Dictionary record) {
		
		return dictionaryVisitorClient.list(record);
	}

	@Override
	public List<DictionaryData> getDataByType(String type) {
		
		return dictionaryDataVisitorClient.getDataByType(type);
	}

	@Override
	public int selectIsSysByType(String type) {
	
		return dictionaryVisitorClient.selectIsSysByType(type);
	}

	@Override
	public List<DictionaryData> findempJobName() {
	
		return dictionaryDataVisitorClient.findempJobName();
	}

	@Override
	public List<DictionaryData> findDictionaryType(String type) {
		
		return dictionaryDataVisitorClient.findDictionaryType(type);
	}


	@Override
	public RestResponseBean getSelectDicPageDate(String pageSize, String currentPage, String searchName,String dicKey) {
		
		RestResponseBean rest=RestResponseBean.getRestResponseBean();
		ResponseBean responseBean=null;
		try{
			responseBean=dictionaryDataVisitorClient.getSelectDicPageDate(Integer.parseInt(pageSize), Integer.parseInt(currentPage), searchName,dicKey);
			if(StringUtils.equals("2", responseBean.getType())){
				rest.setCount((Integer)responseBean.getObject("resultCount"));
				rest.setData((List<DictionaryData>)responseBean.getObject("resultList"));
	        }else{
	        	rest.setCount(0);
	        	rest.setData(new ArrayList<DictionaryData>());
	        }
		}catch(Exception e){
			rest.setCount(0);
        	rest.setData(new ArrayList<DictionaryData>());
		}
		rest.success();
		return rest;
	}



	

}
